//
//  LoginViewController.m
//  OCPB
//
//  Created by Killerp8ss on 6/29/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "LoginViewController.h"
#import "UserInfoViewController.h"
#import "RegisterViewController.h"
#import <CommonCrypto/CommonDigest.h>
//#import "NSData+Base64.h"

@interface LoginViewController ()

@end

@implementation LoginViewController


NSString* API_Key = @"Bny8p47kBs3WnlFceLkhcY9j6x88bsk6";
NSString* Secret_Key = @"j7zz(;W6R05%c=*227w0q53>|{s#;8";
NSString* timeStampObj1= @"1314592551";
NSMutableData* responseData;
NSString* authorizationData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pw_maze_white.png"]];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)LoginClick:(id)sender
{

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSError *error = nil;
    NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    NSString* error_code = [jsonObject valueForKey:@"error_code"];
    
    NSString *jsonRequest =  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSLog(@"jsonResponse is %@", jsonRequest);
    
    if(error_code.intValue > 0)
    {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"OCPB!"
                                                          message:[jsonObject valueForKey:@"message"]
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];
        
    }
    else
    {
        [self performSegueWithIdentifier:@"loginToUserInfo" sender:self];
    }
}

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    if ([string isEqualToString:@"\n"])
    {
        [textField resignFirstResponder];
    }
    return YES;
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //NSLog(@"test");
    UserInfoViewController *u = (UserInfoViewController*)segue.destinationViewController;
    u.authorizationData = authorizationData;   
}
@end
